<!DOCTYPE html>
<!--
Copyright (c) 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/base/math/quad.html">
<link rel="import" href="/tracing/base/math/rect.html">
<script>
'use strict';

function assertQuadEquals(a, b, opt_message) {
  let ok = true;
  ok &= a.p1[0] === b.p1[0] && a.p1[1] === b.p1[1];
  ok &= a.p2[0] === b.p2[0] && a.p2[1] === b.p2[1];
  ok &= a.p3[0] === b.p3[0] && a.p3[1] === b.p3[1];
  ok &= a.p4[0] === b.p4[0] && a.p4[1] === b.p4[1];
  if (ok) return;
  const message = opt_message || 'Expected "' + a.toString() +
      '", got "' + b.toString() + '"';
  assert.fail(a, b, message);
}

tr.b.unittest.testSuite(function() {
  test('pointInTri', function() {
    const res = tr.b.math.pointInTriangle2(
        [0.25, 0.25],
        [0, 0],
        [1, 0],
        [0, 1]);
    assert.isTrue(res);
  });

  test('pointNotInTri', function() {
    const res = tr.b.math.pointInTriangle2(
        [0.75, 0.75],
        [0, 0],
        [1, 0],
        [0, 1]);
    assert.isFalse(res);
  });

  test('pointInside', function() {
    const q = tr.b.math.Quad.from4Vecs([0, 0],
        [1, 0],
        [1, 1],
        [0, 1]);
    const res = q.pointInside([0.5, 0.5]);
    assert.isTrue(res);
  });

  test('pointNotInQuad', function() {
    const q = tr.b.math.Quad.from4Vecs([0, 0],
        [1, 0],
        [1, 1],
        [0, 1]);
    const res = q.pointInside([1.5, 0.5]);
    assert.isFalse(res);
  });

  test('isRectangle', function() {
    assert.isTrue(tr.b.math.Quad.fromXYWH(0, 0, 10, 10).isRectangle());
    assert.isTrue(tr.b.math.Quad.fromXYWH(-10, -10, 5, 5).isRectangle());
    assert.isTrue(tr.b.math.Quad.fromXYWH(-10, -10, 20, 20).isRectangle());
    assert.isTrue(tr.b.math.Quad.fromXYWH(-10, 10, 5, 5).isRectangle());

    assert.isFalse(tr.b.math.Quad.fromXYWH(0, 0, -10, -10).isRectangle());
    assert.isFalse(
        tr.b.math.Quad.from8Array([0, 1, 2, 3, 4, 5, 6, 7]).isRectangle());
    assert.isFalse(
        tr.b.math.Quad.from8Array([0, 0, 0, 5, 5, 5, 0, 0]).isRectangle());
  });

  test('projectUnitRect', function() {
    const container = tr.b.math.Quad.fromXYWH(0, 0, 10, 10);
    const srcRect = tr.b.math.Rect.fromXYWH(0.1, 0.8, 0.8, 0.1);
    const expectedRect = srcRect.scale(10);

    const q = new tr.b.math.Quad();
    container.projectUnitRectFast(q, srcRect);

    assertQuadEquals(tr.b.math.Quad.fromRect(expectedRect), q);
  });

  test('projectUnitRectOntoUnitQuad', function() {
    const container = tr.b.math.Quad.fromXYWH(0, 0, 1, 1);
    const srcRect = tr.b.math.Rect.fromXYWH(0.0, 0, 1, 1);
    const expectedRect = srcRect;

    const q = new tr.b.math.Quad();
    container.projectUnitRectFast(q, srcRect);

    assertQuadEquals(tr.b.math.Quad.fromRect(expectedRect), q);
  });

  test('projectUnitRectOntoSizeTwoQuad', function() {
    const container = tr.b.math.Quad.fromXYWH(0, 0, 2, 2);
    const srcRect = tr.b.math.Rect.fromXYWH(0.0, 0, 1, 1);
    const expectedRect = srcRect.scale(2);

    const q = new tr.b.math.Quad();
    container.projectUnitRectFast(q, srcRect);

    assertQuadEquals(tr.b.math.Quad.fromRect(expectedRect), q);
  });

  test('projectUnitRectOntoTranslatedQuad', function() {
    const container = tr.b.math.Quad.fromXYWH(1, 1, 1, 1);
    const srcRect = tr.b.math.Rect.fromXYWH(0.0, 0, 1, 1);
    const expectedRect = srcRect.translate([1, 1]);

    const q = new tr.b.math.Quad();
    container.projectUnitRectFast(q, srcRect);

    assertQuadEquals(tr.b.math.Quad.fromRect(expectedRect), q);
  });

  test('projectShrunkUnitRectOntoUnitQuad', function() {
    const container = tr.b.math.Quad.fromXYWH(0, 0, 1, 1);
    const srcRect = tr.b.math.Rect.fromXYWH(0.1, 0.1, 0.8, 0.8);
    const expectedRect = srcRect;

    const q = new tr.b.math.Quad();
    container.projectUnitRectFast(q, srcRect);

    assertQuadEquals(tr.b.math.Quad.fromRect(expectedRect), q);
  });
});
</script>
